package com.cloud;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.Data;

import java.io.BufferedWriter;
import java.io.File;
import java.nio.charset.Charset;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;


public class ReMain {

    public static final String configName = "config.json";


    public static final String MYSQL_REX = "host=\"(.*?)\" port=\"(.*?)\" user=\"(.*?)\" pwd=\"(.*?)\" dbname=";

    public static final String REDIS_REX = "host=\"(.*?)\" port=\"(.*?)\" pwd=\"(.*?)\" db=\"(.*?)\"";

    public static final String SERVER_IP_REX = "host=\"(.*?)\" inner_ip=\"(.*?)\" outer_ip=\"(.*?)\"";

    public static final String NET_REX = "<Connect app_id=\"(.*?)\" ip=\"(.*?)\" port=\"(.*?)\"/>";

    public static final String MONGO_REX = "\"connectionUri\": \"mongodb://(.*?):(.*?)\",";

    public static final String GAME_SERVER_REX = "\"DispatchUrl\": \"http://(.*?):20001/query_cur_region\"";

    public static final List<String> xmlFileNames = Arrays.asList(
            new String[]{
                    "dbgate/conf/dbgate.xml",
                    "dispatch/conf/dispatch.xml",
                    "gameserver/conf/gameserver.xml",
                    "gateserver/conf/gateserver.xml",
                    "muipserver/conf/muipserver.xml",
                    "multiserver/conf/multiserver.xml",
                    "nodeserver/conf/nodeserver.xml",
                    "oaserver/conf/oaserver.xml",
                    "pathfindingserver/conf/pathfindingserver.xml",
                    "tothemoonserver/conf/tothemoonserver.xml"
            }
    );

    public static final String sdkConfigJsonName = "sdkserver/config.json";

    public static void main(String[] args) {
        String property = System.getProperty("user.dir");
        File thisConfigFile = new File(property, configName);
        JSONObject jsonObject = JSONUtil.readJSONObject(thisConfigFile, Charset.defaultCharset());
        Config config = JSONUtil.toBean(jsonObject, Config.class);
        String srvPath = config.getSrvPath();
        MysqlConfig mysql = config.getMysql();
        RedisConfig redis = config.getRedis();
        ServerIpConfig server = config.getServer();
        MongoConfig mongo = config.getMongo();


        String serverConfig = "host=\"" + server.getHost() + "\" inner_ip=\"" + server.getInnerIp() + "\" outer_ip=\"" + server.getOuterIp() + "\"";
        String netConfig = "<Connect app_id=\"$1\" ip=\"" + server.getInnerIp() + "\" port=\"$3\"/>";
        String mysqlConfig = "host=\"" + mysql.getHost() + "\" port=\"" + mysql.getPort() + "\" user=\"" + mysql.getUser() + "\" pwd=\"" + mysql.getPwd() + "\" dbname=";
        String redisConfig = "host=\"" + redis.getHost() + "\" port=\"" + redis.getPort() + "\" pwd=\"" + redis.getPwd() + "\" db=\"" + redis.getDb() + "\"";


        String mongoConfig = "\"connectionUri\": \"mongodb://" + mongo.getHost() + ":" + mongo.getPort() + "\",";
        String gameServerConfig = "\"DispatchUrl\": \"http://" + server.getOuterIp() + ":20001/query_cur_region\"";


        for (String fileName : xmlFileNames) {
            FileReader fileReader = FileReader.create(new File(srvPath, fileName));
            FileWriter fileWriter = FileWriter.create(new File(srvPath, fileName));
            replaceIp(SERVER_IP_REX, serverConfig, fileReader, fileWriter);
            replaceIp(NET_REX, netConfig, fileReader, fileWriter);
            replaceIp(MYSQL_REX, mysqlConfig, fileReader, fileWriter);
            replaceIp(REDIS_REX, redisConfig, fileReader, fileWriter);
        }

        FileReader fileReader = FileReader.create(new File(srvPath, sdkConfigJsonName));
        FileWriter fileWriter = FileWriter.create(new File(srvPath, sdkConfigJsonName));
        replaceIp(MONGO_REX, mongoConfig, fileReader, fileWriter);
        replaceIp(GAME_SERVER_REX, gameServerConfig, fileReader, fileWriter);

    }

    private static boolean replaceIp(String oldIpLine, String newIpLine, FileReader fileReader, FileWriter fileWriter) {
        List<String> lineStr = fileReader.readLines();
        AtomicBoolean atomicBoolean = new AtomicBoolean(false);
        List<String> newLineStr = lineStr.stream()
                .map(str -> {
                    if (ReUtil.contains(oldIpLine, str)) {
                        atomicBoolean.set(true);
                        return ReUtil.replaceAll(str, oldIpLine, newIpLine);
                    }
                    return str;
                })
                .collect(Collectors.toList());
        if (atomicBoolean.get()) {
            System.out.println("修改 文件Ip:" + fileReader.getFile().getAbsolutePath() + " 成功~");
            System.out.println("已经 修改成:" + newIpLine);
            fileWriter.writeLines(newLineStr);
            return true;
        }
        return false;
    }


    @Data
    class Config {
        private MysqlConfig mysql;
        private RedisConfig redis;
        private MongoConfig mongo;
        private ServerIpConfig server;
        private String srvPath = "/root/genshin/srv";


    }

    @Data
    class MysqlConfig {
        private String host = "127.0.0.1";
        private Integer port = 3306;
        private String user = "root";
        private String pwd = "123456";


    }

    @Data
    class RedisConfig {
        private String host = "127.0.0.1";
        private Integer port = 6379;
        private String pwd = "123456";
        private Integer db = 1;

    }

    @Data
    class ServerIpConfig {
        private String host = "127.0.0.1";
        private String outerIp = "127.0.0.1";
        private String innerIp = "127.0.0.1";
    }

    @Data
    class MongoConfig {
        private String host = "127.0.0.1";
        private Integer port = 27017;
    }
}
